<template>
  <div class="managerMana-container common-container">
    <div class="common-top">
      <span class="left title">账号管理</span>
      <el-button type="primary" size="small" @click="doModify('add')">添加账号</el-button>
    </div>
    <div class="common-bot">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="ID">
          <template slot-scope="scope">
            <Briefly :value="scope.row.id" :length="10"/>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column prop="gender" label="性别">
          <template slot-scope="scope">
            <span>{{ gender[scope.row.gender] }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="mobile" label="手机号"></el-table-column>
        <el-table-column
          prop="enable"
          label="是否启用"
          v-if="hasPerm('system:account:enable')">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enable"
              @change="doModify('enable', scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="roleGroups" label="权限组">
          <template slot-scope="scope">
            <template v-if="scope.row.roleGroups.length">
              <div v-for="(item, index) in scope.row.roleGroups" :key="index">
                {{ item.name }}
              </div>
            </template>
            <template v-else>
              <div>---</div>
            </template>
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作"
          v-if="hasPerm('system:account:edit', 'system:account:del')">
          <template slot-scope="scope">
            <span
              class="span-modify edit"
              v-if="hasPerm('system:account:edit')"
              @click.stop="doModify('edit', scope.row)">编辑</span>
            <span
              class="span-modify del"
              v-if="hasPerm('system:account:del')"
              @click.stop="doModify('del', scope.row)">删除</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          v-if="showPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="nowPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="totalElement"
        ></el-pagination>
      </div>
    </div>
    <Modify
      @close="close"
      :id="modifyId"
      :type="modifyType"
      :showModify="showModify"
    />
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import Api from "@/api/index";
import Briefly from "@/layout/components/Briefly";
import Modify from "./modify.vue";

export default {
  components: {Briefly, Modify},
  computed: {
    ...mapGetters(["name"]),
  },
  data() {
    return {
      modifyType: "add",
      modifyId: "",
      showModify: false,
      //
      showPage: false,
      tableData: [],
      enable: 0,
      keyword: "",
      limit: 10,
      nowPage: 1,
      roleGroupId: 0,
      gender: {
        female: "女",
        male: "男",
      },
      totalElement: 0,
    };
  },
  watch: {
    nowPage(val) {
      this.getData();
      this.setStateData(val);
    },
    limit(val) {
      this.getData();
    },
  },
  methods: {
    close() {
      this.showModify = false;
      this.getData();
    },
    //
    setStateData(nowPage) {
      let stateData = {
        nowPage,
      };
      this.$router.replace({
        path: this.$route.path,
        query: {
          stateData: this.$base64.encode(JSON.stringify(stateData)),
        },
      });
    },
    handleSizeChange(val) {
      this.limit = val;
    },
    handleCurrentChange(val) {
      this.nowPage = val;
    },
    doModify(type, item = null) {
      switch (type) {
        case "add":
          this.modifyId = "";
          this.modifyType = type;
          this.showModify = true;
          break;
        case "edit":
          this.modifyId = item.id;
          this.modifyType = type;
          this.showModify = true;
          break;
        case "enable":
          Api.manager.enable({
            targetId: item.id,
            yes: item.enable ? true : false,
          }).then((res) => {
            if (res.code == 200) {
              this.$message.success("操作成功");
              this.getData();
            }
          })
            .catch((e) => this.getData());
          break;
        case "del":
          this.$confirm("确认此操作?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            Api.manager.del({targetIds: [item.id]}).then((res) => {
              if (res.code == 200) {
                this.$message.success("操作成功");
                this.getData();
              }
            });
          }).catch((e) => this.getData());
          break;
      }
    },
    getData() {
      Api.manager.list({
        enable: this.enable,
        keyword: this.keyword,
        limit: this.limit,
        page: this.nowPage - 1,
        roleGroupId: this.roleGroupId,
      }).then((res) => {
        this.tableData = res.data;
        this.totalElement = res.total;
        this.showPage = true;
      });
    },
  },
  mounted() {
    if (this.$route.query.stateData) {
      const stateData = JSON.parse(
        this.$base64.decode(this.$route.query.stateData)
      );
      this.nowPage = stateData.nowPage;
      this.getData();
    } else {
      this.getData();
    }
  },
};
</script>

<style lang="scss" scoped></style>
